<template>
  <div class="addform">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-position="right"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="项目名称:" prop="itemName">
        <el-input v-model="ruleForm.itemName" class="inputS"></el-input>
      </el-form-item>
      <el-form-item label="项目完成时间:" prop="itemDate">
        <el-date-picker v-model="ruleForm.itemDate" type="date">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目介绍第一段:" prop="itemFirst">
        <el-input
          type="textarea"
          v-model="ruleForm.itemFirst"
          :rows="4"
          class="inputL"
        ></el-input>
      </el-form-item>
      <el-form-item label="默认图片:">
        <el-switch v-model="imgTF"></el-switch>
      </el-form-item>
      <el-form-item label="图片选择:" v-show="imgTF">
        <el-radio v-model="radioImg" label="1"> 重要图片 </el-radio>
        <el-radio v-model="radioImg" label="2"> 无图片 </el-radio>
      </el-form-item>
      <!-- 图片1 -->
      <el-form-item label="上传图片1:" prop="itemImg1" v-show="!imgTF">
        <el-upload
          :action="this.uploadIndex2"
          list-type="picture-card"
          ref="upload1"
          :auto-upload="false"
          :on-change="
            (file) => {
              beforeAvatarUpload(1, file);
            }
          "
          :on-success="
            (res, file) => {
              handleAvatarSuccess(1, res, file);
            }
          "
        >
          <i slot="default" class="el-icon-plus"></i>
          <div class="el-upload__tip" slot="tip">
            只能上传jpg/png文件，且不超过20mb
          </div>
          <!-- 图片 -->
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="Pview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>
            </span>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="项目介绍第二段:" prop="itemSecond">
        <el-input
          type="textarea"
          v-model="ruleForm.itemSecond"
          :rows="4"
          class="inputL"
        ></el-input>
      </el-form-item>
      <!-- 图片2 -->
      <el-form-item label="上传图片2:" prop="itemImg2" v-show="!imgTF">
        <el-upload
          :action="this.uploadIndex2"
          list-type="picture-card"
          ref="upload1"
          :auto-upload="false"
          :on-change="
            (file) => {
              beforeAvatarUpload(2, file);
            }
          "
          :on-success="
            (res, file) => {
              handleAvatarSuccess(2, res, file);
            }
          "
        >
          <i slot="default" class="el-icon-plus"></i>
          <div class="el-upload__tip" slot="tip">
            只能上传jpg/png文件，且不超过20mb
          </div>
          <!-- 图片 -->
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="Pview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>
            </span>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >新增项目</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 图片预览 -->
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imgTF: false,
      radioImg: "",
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      successVisible: false,
      ruleForm: {//保证前后端名称相同
        itemName: "",
        itemFirst: "",
        itemSecond: "",
        itemImg1: "",
        itemImg2: "",
        itemDate: ""
      },

      rules: {//如果没输入的提示信息
        itemName: [
          { required: true, message: "请输入项目名", trigger: "blur" },
          { max: 50, message: "最大长度50个字", trigger: "blur" },
        ],
        itemDate: [
          { required: true, message: "请输入完成时间", trigger: "blur" }
        ]
      },
    };
  },
  methods: {
    // 预览 无需管
    Pview(file) {
      console.log(file.url);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 上传图片判断
    beforeAvatarUpload(idx, file) {
      console.log(idx, file);
      const isLt10M = file.size / 1024 / 1024 < 10;
      console.log("文件大小:" + (file.size / 1024 / 1024).toFixed(2) + "MB");
      if (!isLt10M) {
        this.$message.error("上传图片大小不能超过 10MB!");
        if (idx == 1) {
          this.$refs.upload1.clearFiles();
        }
        if (idx == 2) {
          this.$refs.upload2.clearFiles();
        }
      } else {
        if (idx == 1) {
          this.$refs.upload1.submit();
        }
        if (idx == 2) {
          this.$refs.upload2.submit();
        }
        this.radioImg = '3';
      }
      return isLt10M;
    },
    //上传
    handleAvatarSuccess(idx, res, file) {
      console.log(idx, res, file);
      this.disabled = true;
      if (idx == 1) {
        this.ruleForm.itemImg1 = "/assets/myIndex2/img/" + file.name;
        console.log("图片1:" + this.ruleForm.itemImg1);
      }
      if (idx == 2) {
        this.ruleForm.itemImg2 = "/assets/myIndex2/img/" + file.name;
        console.log("图片2:" + this.ruleForm.itemImg2);
      }
    },
    submitForm(formName) {
      if (this.radioImg = '1') {
        this.ruleForm.itemImg1 = "/assets/myIndex2/img/秘密图.png"
        this.ruleForm.itemImg2 = "/assets/myIndex2/img/秘密图.png"
      }
      if (this.radioImg = '2') {
        this.ruleForm.itemImg1 = "/assets/myIndex2/img/没有图哦.png"
        this.ruleForm.itemImg2 = "/assets/myIndex2/img/没有图哦.png"
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 由于axios内部必须使用_this才能使用this
          const _this = this;
          this.axios({
            method: "post",
            url: _this.baseUrl + "/index2/post/addItem",
            //传递给后端的数据
            data: _this.ruleForm
          }).then(function (resp) {
            console.log(resp.data);
            _this.$parent.$parent.successAdd();//提交成功提示
          });
        } else {
          console.log("新增失败");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
};
</script>

<style lang="less" scoped>
.addform {
  width: 100%;
  margin: 0 auto;
  .inputS {
    width: 80%;
  }
  .inputL {
    width: 80%;
    display: block;
  }
  .imgPreview {
    width: 150px;
    height: 150px;
    margin-right: 15px;
    float: left;
    cursor: pointer;
  }
}
</style>